package com.project.common.controller;

import com.alibaba.fastjson.JSONObject;
import com.project.common.entity.PageVo;
import com.project.common.entity.SysMenuRole;
import com.project.common.entity.SysRoles;
import com.project.common.entity.SysUserRole;
import com.project.common.service.SysRoleUserService;
import com.project.common.service.SysRolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sysUserRoles")
public class SysUserRoleController {
    @Autowired
    private SysRoleUserService sysRoleUserService;

    @PostMapping("/grantAndRevokeUserFromRole.action")
    public int grantAndRevokeMenuFromRole(String roleIdsStr,Integer userId,String roleIdsNoCheck){
        Integer[] roleIds = JSONObject.parseObject(roleIdsStr, Integer[].class);
        Integer[] roleNoCheckIds = JSONObject.parseObject(roleIdsNoCheck, Integer[].class);
        //删除角色对应的所有菜单
        if(roleNoCheckIds!=null){

            for(int i=0;i<roleNoCheckIds.length;i++) {

                sysRoleUserService.deleteByUserIdAndRoleId(userId,roleNoCheckIds[i]);
            }
        }
        //添加勾选的菜单
        if(roleIds!=null){

            for(int i=0;i<roleIds.length;i++){
                List<SysUserRole> sysUserRoles = sysRoleUserService.selectByUserIdAndRoleId(userId, roleIds[i]);
                if(sysUserRoles.size()<=0){

                    SysUserRole sysUserRole = new SysUserRole();
                    sysUserRole.setRoleId(roleIds[i]);
                    sysUserRole.setUserId(userId);
                    sysRoleUserService.insertSelective(sysUserRole);
                }
            }
        }
        return 0;
    }


}
